package com.fick.tags;

/**
 * 显示指定channelid的栏目下的信息列表
 */
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.common.ContantsProperties;
import com.fick.hibernate.dao.ChannelDAO;
import com.fick.hibernate.dao.NewsDAO;
import com.fick.hibernate.model.Channel;
import com.fick.hibernate.model.News;
import com.fick.util.General;
import com.fick.util.Pager;

@SuppressWarnings("serial")
public class ProductIndexTag extends TagSupport {
	private static Log log = LogFactory.getLog(ProductIndexTag.class);

	// 是否带栏目简介
	private boolean isDesc = true;
	// 显示几条
	private int num = 9;
	
	private int rownum = 3;
	// 标题长度
	private int length = 30;
	// <a>标签href页面
	private String linkPage = "info.jsp";
	// <a>标签属性
	private String linkAttr = "";
	
	private String imgAttr = "";
	
	private String rowB = "";
	
	private String rowE = "";
	
	private String proB = "";
	
	private String proE = "";
	// 分页表格table属性
	private String pageTableAttr = "";
	// 分布表格td属性
	private String pageTdAttr = "";

	public int doStartTag() throws JspException {

		StringBuffer sb = new StringBuffer("");
		String defaultimgurl = ContantsProperties.getContantsProperties().getStringProperty("DEFAULT_PRODUCT_PIC");

		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();
		String channelidS = request.getParameter("channelid");
		if (channelidS != null && channelidS.trim().length() > 0) {
			int channelid = Integer.parseInt(channelidS);
			int currpage = 1;
			int totalpage = 0;
			int totalcount = 0;
			int pagesize = num;
			Channel channel = new ChannelDAO().findById(channelid);
			if (channel == null) {
				doNull(sb);
			} else {
				NewsDAO dao = new NewsDAO();
				totalcount = dao.count(News.class,"where isAss='1' and channelid.channelId=" + channelid);
				if(totalcount > 0){
					List<News> list = null;
					list = dao.findPageByHql("where isAss='1' and channelid.channelId=" + channelid + " order by addDate desc",currpage, num);
				if (totalcount == 1) {
					doOneNews(sb,list.get(0));
				} else {
					// 得到总的页数
					if (totalcount % pagesize == 0) {
						totalpage = totalcount / pagesize;
					} else {
						totalpage = totalcount / pagesize + 1;
					}
					if (request.getParameter("currPage") != null) {
						currpage = Integer.parseInt(request
								.getParameter("currPage"));
						if (currpage < 1) {
							currpage = 1;
						}
						if (currpage > totalpage) {
							currpage = totalpage;
						}
					}
					sb.append("<h1>");
					sb.append(channel.getName());
					sb.append("</h1>");
					if (isDesc && channel.getChdescribe() != null && !"".equals(channel.getChdescribe())) {
						sb.append(channel.getChdescribe());
					}
					News news = null;
					String linktemp="";
					String imgurltemp = "";
					for(int i = 0 ; i < list.size() ; i ++){
						news = list.get(i);
					if(i % rownum == 0){
						sb.append("<");
						sb.append(rowB);
						sb.append(">");
					}

					sb.append("<");
					sb.append(proB);
					sb.append(">");
					linktemp = "<a "+linkAttr+" href='"+linkPage + "?channelid="+channelid+"&newsid="+news.getNewId()+"'>";
					imgurltemp = StringUtils.isEmpty(news.getImg()) ? defaultimgurl : news.getImg();
					sb.append(linktemp);
					sb.append("<img "+imgAttr+" src='");
					sb.append(imgurltemp);
					sb.append("' />");
					sb.append("</a>");
					sb.append(linktemp);
					sb.append(StringUtils.abbreviate(news.getTitle(), length));
					sb.append("</a>");

					sb.append("</");
					sb.append(proE);
					sb.append(">");
					if(i % rownum == rownum -1 || i == list.size() -1){
						sb.append("</");
						sb.append(rowE);
						sb.append(">");
					}
					
					}
					
					// 显示分页代码
					sb.append("<table ");
					sb.append(pageTableAttr);
					sb.append(" >");
					sb.append("<tr>");
					sb.append("<td ");
					sb.append(pageTdAttr);
					sb.append(" >");
					sb.append(Pager.nprintTable(channel.getUrl()
							+ "?channelid=" + channelid, currpage, totalpage,
							totalcount, "&", ""));
					sb.append("</td>");
					sb.append("</tr>");
					sb.append("</table></div>");
				}
			}else {
					doNull(sb);
				}
			} 
		} else {
			doNull(sb);
		}

		JspWriter out = pageContext.getOut();
		try {
			out.println(sb.toString());
			out.flush();
		} catch (IOException e) {
			log.error("标签显示错误", e);
		}
		return SKIP_BODY;
	}

	private void doOneNews(StringBuffer sb,News news) {
		sb.append("<h1>");
		sb.append(news.getTitle());
		sb.append("</h1>");
		sb.append(news.getContent());
	}

	private void doNull(StringBuffer sb) {
		sb.append("<p>");
		sb.append("Thers is not a right channelid neither a newsid!");
		sb.append("</p>");
	}

	public boolean isDesc() {
		return isDesc;
	}

	public void setDesc(boolean isDesc) {
		this.isDesc = isDesc;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getRownum() {
		return rownum;
	}

	public void setRownum(int rownum) {
		this.rownum = rownum;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public String getLinkPage() {
		return linkPage;
	}

	public void setLinkPage(String linkPage) {
		this.linkPage = linkPage;
	}

	public String getLinkAttr() {
		return linkAttr;
	}

	public void setLinkAttr(String linkAttr) {
		this.linkAttr = linkAttr;
	}

	public String getImgAttr() {
		return imgAttr;
	}

	public void setImgAttr(String imgAttr) {
		this.imgAttr = imgAttr;
	}

	public String getRowB() {
		return rowB;
	}

	public void setRowB(String rowB) {
		this.rowB = rowB;
	}

	public String getRowE() {
		return rowE;
	}

	public void setRowE(String rowE) {
		this.rowE = rowE;
	}

	public String getProB() {
		return proB;
	}

	public void setProB(String proB) {
		this.proB = proB;
	}

	public String getProE() {
		return proE;
	}

	public void setProE(String proE) {
		this.proE = proE;
	}

	public String getPageTableAttr() {
		return pageTableAttr;
	}

	public void setPageTableAttr(String pageTableAttr) {
		this.pageTableAttr = pageTableAttr;
	}

	public String getPageTdAttr() {
		return pageTdAttr;
	}

	public void setPageTdAttr(String pageTdAttr) {
		this.pageTdAttr = pageTdAttr;
	}

}
